<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>用户注册</title>
    <meta name="keywords" content="" />
    <meta name="description" content="">
    <link href="css/bulma.min.css" rel="stylesheet">
    <link href="css/github-markdown.min.css" rel="stylesheet">
    <link rel="stylesheet" href="css/app.css">

    <script src="./js/config.js"></script>
    <script src="./js/jquery.min.js"></script>
</head>

<body>
    <nav id="navbar" class="navbar has-shadow">
        <div class="container">
            <div id="navMenu" class="navbar-menu">
                <div class="navbar-start">
                    <a class="navbar-item" href="index.html">主页</a>
                    <a class="navbar-item" href="about.html">关于</a>
                </div>
                <div class="navbar-end">
                    <div class="navbar-item">
                        <a class="button is-small is-primary" href="register.html">注册</a>
                    </div>
                    <div class="navbar-item">
                        <a class="button is-small" href="login.html">登录</a>
                    </div>
                </div>
            </div>
        </div>
    </nav>

    <div class="container">
        <div class="box sign-box">
            <div>
                <div class="field">
                    <label class="label">用户名</label>
                    <div class="control">
                        <input class="input" id="username" type="text" placeholder="请输入用户名">
                    </div>
                </div>
                <div class="field">
                    <label class="label">邮箱</label>
                    <div class="control">
                        <input class="input" id="email" type="email" placeholder="请输入你的邮箱">
                    </div>
                </div>
                <div class="field">
                    <label class="label">密码</label>
                    <div class="control">
                        <input class="input" id="password" type="password" placeholder="请输入密码">
                    </div>
                </div>
                <div class="field">
                    <label class="label">重复密码</label>
                    <div class="control">
                        <input class="input" id="repassword" type="password" placeholder="确认你的密码">
                    </div>
                </div>
                <div class="field is-grouped">
                    <div class="control">
                        <button class="button is-primary" onclick="OnRegister()">立即注册</button>
                    </div>
                    <div class="control">
                        <a href="login.html" class="button is-text">已有账号，去登录</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>

<script>
    function OnRegister() {
        // 1、先进行参数的非空校验
        var username = jQuery("#username");
        if (username.val().trim() == "") {
            // 此时用户名为空
            alert("当前用户名为空!");
            username.focus();
            return;
        }

        var email = jQuery("#email");
        if (email.val().trim() == "") {
            // 此时用户名为空
            alert("当前用户邮箱为空!");
            email.focus();
            return;
        }

        var password = jQuery("#password");
        if (password.val().trim() == "") {
            // 此时用户名为空
            alert("当前用户密码为空!");
            password.focus();
            return;
        }

        var repassword = jQuery("#repassword");
        if (repassword.val().trim() == "") {
            // 此时用户名为空
            alert("当前确认密码为空!");
            repassword.focus();
            return;
        }
        // 2、进行密码的一致性判断
        if (password.val() != repassword.val()) {
            alert("两次密码输入不一致!");
            repassword.focus();
            return;
        }
        // 3、拿到注册到结果
        jQuery.ajax({
            url: base_url + "/user/add",
            method: "POST",
            data: {
                "username": username.val(),
                "email": email.val(),
                "password": password.val()
            },
            // 结果的返回 code、msg、data
            success: function (result) {
                if (result != null && result.code == 200 && result.data == "") {
                    alert("用户注册成功！");
                    location = "./login.html";
                    return;
                }
                else {
                    alert("用户注册失败：" + result.msg);
                    return;
                }
            }
        });
    }
</script>

</html>